package com.sdry.good.goodChain.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.sdry.basic.entity.Material;
import com.sdry.basic.entity.ProductionLine;
import com.sdry.basic.entity.ProductionStation;
import com.sdry.basic.service.MaterialService;
import com.sdry.basic.service.ProductionStationService;
import com.sdry.common.BaseAPIMessageTypeEnum;
import com.sdry.common.BaseTypeEnum;
import com.sdry.good.goodWarehouse.service.GoodSortFrameService;
import com.sdry.good.logistics.entity.ReportPicking;
import com.sdry.good.logistics.service.ReportPickingService;
import com.sdry.good.out.entity.OutStockOrder;
import com.sdry.good.out.entity.OutStockOrderDetail;
import com.sdry.good.out.service.OutStockOrderDetailService;
import com.sdry.good.out.service.OutStockOrderService;
import com.sdry.product.prod.entity.LacquerInto;
import com.sdry.product.prod.entity.ProductTwoPaint;
import com.sdry.product.prod.entity.ResultAppList;
import com.sdry.product.prod.service.LacquerIntoService;
import com.sdry.system.entity.SysUser;
import com.sdry.system.mapper.UserMapper;
import com.sdry.transit.entity.TransitOrder;
import com.sdry.untils.DateUtil;
import com.sdry.untils.ResponseUtil;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.sdry.good.goodChain.entity.GoodChain;
import com.sdry.good.goodChain.service.GoodChainService;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 *
 *@ClassName: GoodChainController
 *@Description: 链条绑定表
 *@Author tdd
 *@Date 2020-03-23 15:24:50
 *@version 1.0
*/
@Controller
@RequestMapping("/goodChain")
public class GoodChainController{
	@Resource GoodChainService goodChainService;
	@Resource
	ProductionStationService productionStationService;
	@Resource
    LacquerIntoService lacquerIntoService;
	@Resource
	ReportPickingService reportPickingService;
	@Resource
	OutStockOrderService outStockOrderService;
	@Resource
	OutStockOrderDetailService outStockOrderDetailService;
	@Resource
	private UserMapper userMapper;
	@Resource
	MaterialService materialService;
	@Resource
	GoodSortFrameService goodSortFrameService;
	//声明一个Logger
	private final static Logger logger = LoggerFactory.getLogger(GoodChainController.class);
	/**
	 * 进入页面
	 * @param model
	 * @return 页面路径
	 */
	@RequestMapping("/pageGoodChain")
	public String pageGoodChain (Model model) {
		return "/td/goodChain/inventory";
	}
	/**
	 * 进入页面
	 * @param model
	 * @return 页面路径
	 */
	@RequestMapping("/pageGoodChainOn")
	public String pageGoodChainOn (Model model) {
		return "/td/goodChain/onChain";
	}
	/**
	 * 进入页面
	 * @param model
	 * @return 页面路径
	 */
	@RequestMapping("/pageGoodChainNuder")
	public String pageGoodChainNuder (Model model) {
		return "/td/goodChain/nuderChain";
	}
	/**
	 * 进入页面
	 * @param model
	 * @return 页面路径
	 */
	@RequestMapping("/pageBinding")
	public String pageBinding (Model model) {
		return "/td/goodChain/binding";
	}


	/** 
	 * 根据条件分页查询
	 * @param param 实体条件
	 * @param response
	*/
	@RequestMapping("/list")
	public void list(GoodChain param,HttpServletResponse response){
		logger.info("根据条件分页查询条件"+JSONArray.fromObject(param));
		/*
		 * 条件分页查询内容
		 */
		List<GoodChain> list = goodChainService.findPageByMution(param);
		logger.info("根据条件分页查询条件返回内容"+JSONArray.fromObject(list));
		/*
		 * 条件查询总条数
		 */
		Integer total = goodChainService.selectCountByMution(param);
		logger.info("条件查询总条数返回条数"+total);
		/*
		 * 转为json数据传至页面
		 */
		//创建result
		JSONObject jsonObject = new JSONObject();
		//添加入jsonObject
		jsonObject.put("code", 0);
		jsonObject.put("rows", list);
		jsonObject.put("total", total);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			logger.info("根据条件分页查询返回数据失败");
			e.printStackTrace();
		}
	}

	/**
	 * 根据条件分页查询库存
	 * @param param 实体条件
	 * @param response
	 */
	@RequestMapping("/listInventory")
	public void listInventory(GoodChain param,HttpServletResponse response){
		logger.info("根据条件分页查询条件"+JSONArray.fromObject(param));
		/*
		 * 条件分页查询内容
		 */
		List<GoodChain> list = goodChainService.findInventoryPageByMution(param);
		logger.info("根据条件分页查询条件返回内容"+JSONArray.fromObject(list));
		/*
		 * 条件查询总条数
		 */
		Integer total = goodChainService.selectInventoryCountByMution(param);
		logger.info("条件查询总条数返回条数"+total);
		/*
		 * 转为json数据传至页面
		 */
		//创建result
		JSONObject jsonObject = new JSONObject();
		//添加入jsonObject
		jsonObject.put("code", 0);
		jsonObject.put("rows", list);
		jsonObject.put("total", total);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			logger.info("根据条件分页查询返回数据失败");
			e.printStackTrace();
		}
	}

	/****************************************app*******************************************/

	/**
	 * app查询物料
	 * @param maNumber 物料编号/名称
	 * @return 实体
	 */
	@RequestMapping("/api/selectMaterial")
	public void selectMaterial(String maNumber,HttpServletResponse response) {
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		String whereStr = "";
		if(null != maNumber && !"".equals(maNumber)){
			whereStr += " and (ma_number like '%" + maNumber+"%' or ma_name like '%" + maNumber+"%')";
		}
		/*Material material = new Material();
		material.setWhereStr(whereStr);
		List<Material> materials = materialService.queryAllByMution(material);*/
		//查询当天生产指令物料
		TransitOrder transitOrder = new TransitOrder();
		transitOrder.setWhereStr(whereStr);
		//查询当天单据
		transitOrder.setTocreateTime(DateUtil.dateFormat1());
		List<Material> materials = goodChainService.selectMaterialByTransitOrder(transitOrder);
		if(materials.size() > 0){
			jsonObject.put("status", "1");
			jsonObject.put("data", materials);
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A001.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * app查询用户
	 * @return 实体
	 */
	@RequestMapping("/api/selectUser")
	public void selectUser(HttpServletResponse response) {
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("del_flag",0L);
		List<SysUser> sysUserList = userMapper.selectList(queryWrapper);
		if(sysUserList.size() > 1){
			jsonObject.put("status", "1");
			jsonObject.put("data", sysUserList);
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A001.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * app根据对应模块的操作产线查询工位 成品链（查喷涂） 成品库（查喷涂、喷漆）
	 * @param permissionType 产线/库区编号
	 * @return 实体
	 */
	@RequestMapping("/api/selectProductionStation")
	public void selectProductionStation(String permissionType,HttpServletResponse response) {
		List<ProductionStation> productionStations = new ArrayList<>();
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		ProductionStation productionStation = new ProductionStation();
		ProductionLine productionLine = new ProductionLine();
		if(permissionType.equals(BaseTypeEnum.CP.getCode())){
			productionLine.setPlnumber(BaseTypeEnum.PT.getCode());
			productionStation.setProductionLine(productionLine);
			productionStations = productionStationService.queryAllByMution(productionStation);
		}else if(permissionType.equals(BaseTypeEnum.CPK.getCode())){
			productionLine.setPlnumber(BaseTypeEnum.PT.getCode());
			productionStation.setProductionLine(productionLine);
			List<ProductionStation> productionStations1 = productionStationService.queryAllByMution(productionStation);
			productionStations.addAll(productionStations1);
			productionLine.setPlnumber(BaseTypeEnum.PQ.getCode());
			productionStation.setProductionLine(productionLine);
			List<ProductionStation> productionStations2 = productionStationService.queryAllByMution(productionStation);
			productionStations.addAll(productionStations2);
		}
		if(productionStations.size() > 1){
			jsonObject.put("status", "1");
			jsonObject.put("data", productionStations);
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A001.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * app根据产线查询工位 打印条码
	 * @param permissionType 产线/库区编号
	 * @return 实体
	 */
	@RequestMapping("/api/selectPrintingProductionStation")
	public void selectPrintingProductionStation(String permissionType,HttpServletResponse response) {
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		ProductionStation productionStation = new ProductionStation();
		ProductionLine productionLine = new ProductionLine();
		productionLine.setPlnumber(permissionType);
		productionStation.setProductionLine(productionLine);
		List<ProductionStation> productionStations = productionStationService.queryAllByMution(productionStation);
		if(productionStations.size() > 1){
			jsonObject.put("status", "1");
			jsonObject.put("data", productionStations);
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A001.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * app查询喷涂线绑定表
     * @param psnumber 工位编号
	 * @param permissionType 产线/库区编号
	 * @return 实体
	 */
	@RequestMapping("/api/selectLacquerInto")
	public void selectLacquerInto(String psnumber,String permissionType,HttpServletResponse response) {
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		if (null != psnumber && !"".equals(psnumber)){
			//分割传入的工位
			String[] psnumberArr= psnumber.split(",");
			//定义集合
			List<ResultAppList> list = new ArrayList<>();
			//遍历工位
			for (int i = 0; i < psnumberArr.length; i++) {
				//定义APP操作集合
				ResultAppList<LacquerInto> resultAppList = new ResultAppList();
				//给赋值
				psnumber = psnumberArr[i];
				//APP操作集合赋值工位
				resultAppList.setPsnumber(psnumber);
				//根据工位查找工位信息
				ProductionStation productionStation = new ProductionStation();
				productionStation.setPsnumber(psnumber);
				//ProductionLine productionLine = new ProductionLine();
				//productionLine.setPlnumber(permissionType);
				//productionStation.setProductionLine(productionLine);
				List<ProductionStation> productionStations = productionStationService.queryAllByMution(productionStation);
				//找到一个工位
				if(productionStations.size() == 1){
					resultAppList.setPsId(productionStations.get(0).getPsid()+"");
					//根据工位id查询喷涂绑定信息
					LacquerInto lacquerInto = new LacquerInto();
					lacquerInto.setStationId(productionStations.get(0).getPsid());
					lacquerInto.setDelFlag("0");
					lacquerInto.setStatus("0");
					//查询喷涂
					List<LacquerInto> lacquerIntos = goodChainService.selectLacquerIntoByMution(lacquerInto);
					//如果喷涂为空且是成品库查询 查询喷漆
					if(lacquerIntos.size() == 0 && permissionType.equals(BaseTypeEnum.CPK.getCode())){
						//查询喷漆并以喷涂带回
						ProductTwoPaint productTwoPaint = new ProductTwoPaint();
						productTwoPaint.setDelFlag("0");
						productTwoPaint.setRemark("1");
						productTwoPaint.setPsId(productionStations.get(0).getPsid());
						lacquerIntos = goodChainService.selectProductTwoPaintZhuangQacquerInto(productTwoPaint);
					}
					//APP操作集合赋值集合
					resultAppList.setList(lacquerIntos);
					//集合添加
					list.add(resultAppList);
				}
			}
			if (list.size() > 0){
				jsonObject.put("status", "1");
				jsonObject.put("data", list);
			}else{
				jsonObject.put("status", "0");
				jsonObject.put("message", BaseAPIMessageTypeEnum.A001.getName());
			}

		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A006.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * app查询喷涂线绑定表 （喷涂出库）
	 * @param psnumber 工位编号
	 * @param permissionType 产线/库区编号
	 * @return 实体
	 */
	@RequestMapping("/api/selectLacquerInto1")
	public void selectLacquerInto1(String psnumber,String permissionType,HttpServletResponse response) {
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		if (null != psnumber && !"".equals(psnumber)){
			//根据工位查找工位信息
			ProductionStation productionStation = new ProductionStation();
			productionStation.setPsnumber(psnumber);
			List<ProductionStation> productionStations = productionStationService.queryAllByMution(productionStation);
			//找到一个工位
			if(productionStations.size() == 1){
				//根据工位id查询喷涂绑定信息
				LacquerInto lacquerInto = new LacquerInto();
				lacquerInto.setStationId(productionStations.get(0).getPsid());
				lacquerInto.setDelFlag("0");
				List<LacquerInto> list = goodChainService.selectLacquerIntoByMution(lacquerInto);
				if (list.size() > 0){
					jsonObject.put("status", "1");
					jsonObject.put("data", list);
				}else{
					jsonObject.put("status", "0");
					jsonObject.put("message", BaseAPIMessageTypeEnum.A001.getName());
				}
			}else{
				jsonObject.put("status", "0");
				jsonObject.put("message", BaseAPIMessageTypeEnum.A013.getName());
			}
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A006.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

    /**
     * app上链条
     * @param json 集合
	 * @param permissionType 产线/库区编号
	 * @param userId 登录人
     * @param response
     */
	@RequestMapping("/api/appGoodChainOn")
	public void appGoodChainOn(String json,String permissionType,Long userId,HttpServletResponse response) {
		//获取登录人
		SysUser user = userMapper.selectById(userId);
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		List<ResultAppList<LacquerInto>> resultAppList = gson.fromJson(json, new TypeToken<List<ResultAppList<LacquerInto>>>() {}.getType());
		if (resultAppList.size() > 0){
            //判断是否成功标识
		    int  flg = 1;
			for (int i = 0; i <resultAppList.size() ; i++) {
			    //获取工位编号
				String psnumber = resultAppList.get(i).getNewPsnumber();
				//获取对应喷涂线物料绑定信息
                List<LacquerInto> lacquerIntos = resultAppList.get(i).getList();
                //根据工位编号查询工位
                ProductionStation productionStation = new ProductionStation();
                productionStation.setPsnumber(psnumber);
				ProductionLine productionLine = new ProductionLine();
				productionLine.setPlnumber(permissionType);
				productionStation.setProductionLine(productionLine);
                List<ProductionStation> productionStations = productionStationService.queryAllByMution(productionStation);
                //判断是否查询到该工位
                if(productionStations.size() == 1){
                    //遍历喷涂线物料绑定信息
                    for (int j = 0; j < lacquerIntos.size(); j++) {
                        //解绑
                        LacquerInto lacquerInto = lacquerIntos.get(j);
						goodSortFrameService.untyingLacquerInto(lacquerInto);
                        //绑定
                        GoodChain goodChain = new GoodChain();
                        goodChain.setPsId(lacquerInto.getStationId());
                        goodChain.setMaId(lacquerInto.getMaterielId());
                        goodChain.setMaNum(lacquerInto.getQuantity());
                        goodChain.setColour(lacquerInto.getSprayingRequirements());
						/*if(null != lacquerInto.getStatus() && !"".equals(lacquerInto.getStatus())){
							goodChain.setQualityStatus(lacquerInto.getStatus());
						}else{
							goodChain.setQualityStatus("0");
						}*/
                        goodChain.setCPsId(productionStations.get(0).getPsid());
                        goodChain.setGcCreateTime(DateUtil.dateFormat3());
                        goodChain.setGcCreateMan(user.getUsername());
                        goodChain.setGcRemark("0");
                        Long gid = goodChainService.insert(goodChain);
                        //判断是否绑定成功
                        if (null == gid || gid <= 0){
                            flg = 0;
                            break;
                        }
                    }
                    if (flg == 0){
                        //上链条失败
                        jsonObject.put("status", "0");
                        jsonObject.put("data", BaseAPIMessageTypeEnum.A016.getName());
                        break;
                    }
                }else{
                    //该工位不存在
                    jsonObject.put("status", "0");
                    jsonObject.put("message", BaseAPIMessageTypeEnum.A013.getName());
                    flg = 0;
                    break;
                }
            }
            //上链条成功
            if (flg > 0){
                jsonObject.put("status", "1");
                jsonObject.put("data", BaseAPIMessageTypeEnum.A015.getName());
            }
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A006.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * app查询领料清单
	 * @param permissionType 产线/库区编号
	 * @param remark 0库位 1工位
	 * @return 实体
	 */
	@RequestMapping("/api/selectReportPickingAllPiNumber")
	public void selectReportPickingAllPiNumber(String permissionType,String remark,HttpServletResponse response) {
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		ReportPicking reportPicking = new ReportPicking();
		//reportPicking.setPermissionType(permissionType);
		reportPicking.setRemark(permissionType);
		reportPicking.setDelFlag("0");
		List<ReportPicking> list = reportPickingService.queryPiNumberAllByMution(reportPicking);
		if (list.size() > 0){
			jsonObject.put("status", "1");
			jsonObject.put("data", list);
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A001.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * app查询领料清单  _暂时不用   返回有工位时使用暂时不用
	 * @param piNumber 领料单编号
	 * @param permissionType 产线/库区编号
	 * @param remark 0库位 1工位
	 * @return 实体
	 */
	@RequestMapping("/api/selectReportPickingGroupWlNumber1")
	public void selectReportPickingGroupWlNumber1(String piNumber,String permissionType,String remark,HttpServletResponse response) {
		List<ResultAppList<ReportPicking>> resultAppLists = new ArrayList<>();
		JSONObject jsonObject = new JSONObject();
		//根据条件查询领料信息
		ReportPicking reportPicking = new ReportPicking();
		reportPicking.setPiNumber(piNumber);
		//reportPicking.setPermissionType(permissionType);
		reportPicking.setRemark(permissionType);
		reportPicking.setDelFlag("0");
		List<ReportPicking> list = reportPickingService.queryAllByMution(reportPicking);
		//将查询的领料信息按工位进行分组
		Map<String, List<ReportPicking>> collect = list.stream().collect(Collectors.groupingBy(ReportPicking::getWlNumber));
		//将map集合改为APP操作集合
		for(Map.Entry<String, List<ReportPicking>> entry:collect.entrySet()){
			ResultAppList<ReportPicking> resultAppList = new ResultAppList<>();
			resultAppList.setPsnumber(entry.getKey());
			resultAppList.setList(entry.getValue());
			resultAppLists.add(resultAppList);
			//System.out.println(entry.getKey()+"--->"+entry.getValue());
		}
		if (resultAppLists.size() > 0){
			jsonObject.put("status", "1");
			jsonObject.put("data", resultAppLists);
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A001.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * app查询领料清单
	 * @param piNumber 领料单编号
	 * @param permissionType 产线/库区编号
	 * @param remark 0库位 1工位
	 * @return 实体
	 */
	@RequestMapping("/api/selectReportPickingGroupWlNumber")
	public void selectReportPickingGroupWlNumber(String piNumber,String permissionType,String remark,HttpServletResponse response) {
		List<ResultAppList<ReportPicking>> resultAppLists = new ArrayList<>();
		JSONObject jsonObject = new JSONObject();
		//根据条件查询领料信息
		ReportPicking reportPicking = new ReportPicking();
		reportPicking.setPiNumber(piNumber);
		//reportPicking.setPermissionType(permissionType);
		reportPicking.setRemark(permissionType);
		reportPicking.setDelFlag("0");
		List<ReportPicking> list = reportPickingService.queryAllByMution(reportPicking);
		if (list.size() > 0){
			jsonObject.put("status", "1");
			jsonObject.put("data", list);
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", BaseAPIMessageTypeEnum.A001.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * app下链条
	 * @param json 领料单
	 * @param out 出库单
	 * @param userId 登录人id
	 * @param response
	 *//*
	@RequestMapping("/api/appGoodChainNuder")
	public void appGoodChainNuder(String json,String out,String userId,HttpServletResponse response) {
		//获取登录人
		SysUser user = userMapper.selectById(userId);
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		List<ReportPicking> reportPickings = gson.fromJson(json, new TypeToken<List<ReportPicking>>() {}.getType());
		OutStockOrder outStockOrder = gson.fromJson(out, new TypeToken<OutStockOrder>() {}.getType());

		if (null != reportPickings && reportPickings.size() > 0){
			int flg = 1;
			//获取某一列的值
			List<Long> idList = reportPickings.stream().map(ReportPicking -> ReportPicking.getPicId()).collect(Collectors.toList());
			String ids = StringUtil.join(idList.toArray(), ",");
			//获取某一列的值
			List<Long> numList = reportPickings.stream().map(ReportPicking -> ReportPicking.getQuantity()).collect(Collectors.toList());
			String nums = StringUtil.join(numList.toArray(), ",");
			//判断是否满足接收的领料信息返回不满足的领料信息
			List<ReportPicking> isnotReportPickings = reportPickingService.isSatisfyReportPickingsByPicIds(ids,nums);
			if(isnotReportPickings.size() > 0){
				jsonObject.put("status", "1");
				jsonObject.put("data", isnotReportPickings.get(0).getWlNumber()+":"+isnotReportPickings.get(0).getMaterial().getMaNumber()+"物料不足");
			}else{
				//新增出库单
				outStockOrder.setCreateBy(user.getUsername());
				outStockOrder.setCreateDate(DateUtil.dateFormat3());
				String outCard = "HY"+ DateUtil.dateFormat4();
				outStockOrder.setOutCard(outCard);
				String osoNumber = "CK"+ DateUtil.dateFormat4();
				outStockOrder.setOsoNumber(osoNumber);
				Long oid = outStockOrderService.insert(outStockOrder);
				flg =reportPickingService.outReportPickingsByPicIds(ids,nums,oid);
				*//*for (int i = 0; i < reportPickings.size(); i++) {
					//领料完成状态修改
					ReportPicking reportPicking = new ReportPicking();
					reportPicking.setPicId(reportPickings.get(i).getPicId());
					reportPicking.setDelFlag("1");
					reportPickingService.update(reportPicking);
					reportPicking = reportPickings.get(i);

					OutStockOrderDetail outStockOrderDetail = new OutStockOrderDetail();
					outStockOrderDetail.setOutStockOrderId(oid);
					outStockOrderDetail.setMaId(reportPicking.getMaterielId());
					outStockOrderDetail.setRdodNum(reportPicking.getQuantity().intValue());
					outStockOrderDetailService.insert(outStockOrderDetail);

					//根据领料单查询成品链绑定信息
					GoodChain param = new GoodChain();
					param.setCPsId(reportPicking.getLocationId());
					param.setMaId(reportPicking.getMaterielId());
					param.setGcRemark("0");
					List<GoodChain> list = goodChainService.queryAllByMution(param);
					Long quantity = reportPicking.getQuantity();
					for (int j = 0; j < list.size(); j++) {
						//解绑
						GoodChain goodChain = new GoodChain();
						goodChain.setGcRemark("1");
						Integer count = goodChainService.update(goodChain);
						if(list.get(j).getMaNum().equals(quantity)){
							break;
						}else if(list.get(j).getMaNum() > quantity){
							goodChain = list.get(j);
							goodChain.setMaNum(goodChain.getMaNum() - quantity);
							goodChain.setGcRemark("0");
							goodChainService.insert(goodChain);
							break;
						}
						quantity = quantity - goodChain.getMaNum();
						if (null == count || count <= 0){
							flg = 0;
							return;
						}
					}

				}*//*
				if (flg > 0){
					jsonObject.put("status", "1");
					jsonObject.put("data", BaseAPIMessageTypeEnum.A017.getName());
				}else{
					jsonObject.put("status", "0");
					jsonObject.put("data", BaseAPIMessageTypeEnum.A018.getName());
				}
			}
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("data", BaseAPIMessageTypeEnum.A006.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/


    /**
     * app查询成品线绑定表
     * @param psnumber 工位编号
     * @return 实体
     */
    @RequestMapping("/api/selectGoodChain")
    public void selectGoodChain(String psnumber,HttpServletResponse response) {
        //json转list集合
        JSONObject jsonObject = new JSONObject();
        if (null != psnumber && !"".equals(psnumber)){
            ProductionStation productionStation = new ProductionStation();
            productionStation.setPsnumber(psnumber);
            List<ProductionStation> productionStations = productionStationService.queryAllByMution(productionStation);
            if(productionStations.size() == 1){
                GoodChain param = new GoodChain();
                param.setCPsId(productionStations.get(0).getPsid());
                param.setGcRemark("0");
                List<GoodChain> list = goodChainService.queryAllByMution(param);
                if (list.size() > 0){
                    jsonObject.put("status", "1");
                    jsonObject.put("data", list);
                }else{
                    jsonObject.put("status", "0");
                    jsonObject.put("message", BaseAPIMessageTypeEnum.A001.getName());
                }
            }else{
                jsonObject.put("status", "0");
                jsonObject.put("message", BaseAPIMessageTypeEnum.A013.getName());
            }
        }else{
            jsonObject.put("status", "0");
            jsonObject.put("message", BaseAPIMessageTypeEnum.A006.getName());
        }
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


	/**
	 * app下链条 ——————————————————————X
	 * @return 实体
	 */
	@RequestMapping("/api/appGoodChainNuder1")
	public void appGoodChainNuder1(Long picId,String json,HttpServletResponse response) {
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		List<GoodChain> goodChains = gson.fromJson(json, new TypeToken<List<GoodChain>>() {}.getType());
		if (null != goodChains && goodChains.size() > 0){
			int flg = 1;
			ReportPicking reportPicking = new ReportPicking();
			reportPicking.setPicId(picId);
			reportPicking.setDelFlag("0");
			reportPickingService.update(reportPicking);
			for (int i = 0; i < goodChains.size(); i++) {
				GoodChain goodChain = new GoodChain();
				goodChain.setGcRemark("1");
				Integer count = goodChainService.update(goodChain);
				if (null == count || count <= 0){
					flg = 0;
					break;
				}
			}
			if (flg > 0){
				jsonObject.put("status", "1");
				jsonObject.put("data", BaseAPIMessageTypeEnum.A017.getName());
			}else{
				jsonObject.put("status", "0");
				jsonObject.put("data", BaseAPIMessageTypeEnum.A018.getName());
			}
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("data", BaseAPIMessageTypeEnum.A006.getName());
		}
		jsonObject = ResponseUtil.jSONObjectAdd(jsonObject,((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
